package hashtable;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: 海琳琦
 * @Date: 2022/1/1 22:15
 * 给定两个数组，编写一个函数来计算它们的交集。
 * https://leetcode-cn.com/problems/intersection-of-two-arrays/
 */
public class Intersection {

    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> result = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                result.add(nums2[i]);
            }
        }
        int[] resArr = new int[result.size()];
        int index = 0;
        for (int i: result) {
            resArr[index++] = i;
        }
        return resArr;
    }

    public static void main(String[] args) {

    }
}
